package cn.baizhi.controller;


import cn.baizhi.entity.City;
import cn.baizhi.service.CityServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LZM
 * @since 2021-12-03
 */
@Controller
@RequestMapping("/cities")
public class CityController {

    @Autowired
    private CityServer cityServer;

    @GetMapping
    public ResponseEntity<List<City>> selectAll(){

        List<City> list = cityServer.list(null);

        return new ResponseEntity<>(list, HttpStatus.OK);
    }

    @PostMapping
    public ResponseEntity<City> add(@RequestBody City city){
        cityServer.save(city);

        return new ResponseEntity<>(city,HttpStatus.OK);
    }

    @GetMapping("/{id}")
    public ResponseEntity<City> selectOne(@PathVariable("id") String id){

        City city = cityServer.getById(id);

        return new ResponseEntity<>(city, HttpStatus.OK);
    }

}

